﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Interop.Word;

namespace LDAPOfficeTool
{
    public partial class ThisAddIn
    {
        public class DocumentState {
            public Word.Document Document { get; set; }
            public LDAPTaskPane Control { get; set; }
            public Microsoft.Office.Tools.CustomTaskPane TaskPane { get; set; }
        }

        public  readonly List<DocumentState> DocumentStates = new List<DocumentState>();
        public static ThisAddIn Instance;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Instance = this;
        }

        void Application_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
        {
            DocumentStates.ForEach(state => { if (state.Document == Doc) RemoveDocumentState(state); });
        }

        void ThisAddIn_NewDocument(Word.Document Doc)
        {
            // wenn man es  beim starten sofort aufmachen möchte
            HandleDocumentOpen(Doc);
        }
           
        void Application_DocumentOpen(Word.Document Doc)
        {
            HandleDocumentOpen(Doc);
        }

        public void AddTaskpane() {
            var document = Application.ActiveDocument;

            HandleDocumentOpen(document);
        }

        public  void HandleDocumentOpen(Word.Document Doc)
        {
            var taskPaneOffen = false;

            DocumentStates.ForEach((state) => {

                string fullName = null;
                try
                {
                    fullName = state.Document.FullName;
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    RemoveDocumentState(state);
                }

                if (state.Document == Doc || fullName == Doc.FullName)
                {
                    if (state.Control.Visible == false || state.Control.Created == false || state.TaskPane.Visible == false)
                    {
                        RemoveDocumentState(state);
                    }
                    else
                    {
                        taskPaneOffen = true;
                    }
                
                }
            });

            if (taskPaneOffen)
                return;

            var control = new LDAPTaskPane();
            var taskPane = CustomTaskPanes.Add(control, "Suche in LDAP.gv.at", Doc.ActiveWindow);
            taskPane.Control.Dock = System.Windows.Forms.DockStyle.Fill;
            taskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
            taskPane.Visible = true;
            DocumentStates.Add(new DocumentState { Document = Doc, Control = control, TaskPane = taskPane });
        }

        private void RemoveDocumentState(DocumentState state)
        {
            try
            {
                state.TaskPane.Visible = false;
            }
            catch (System.Runtime.InteropServices.COMException)
            {
            }

            CustomTaskPanes.Remove(state.TaskPane);
            DocumentStates.Remove(state);
        }


        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
 

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
